home *** CD-ROM | disk | FTP | other *** search
/ AOL File Library: 2,801 to 2,900 / aol-file-protocol-4400-2801-to-2900.zip / AOLDLs / TAWUG / TAWUG Disk No. 56 (SHK) / TAWUG56.shk / BASIC.NOTES (.txt) < prev    next >
AppleWorks Document  |  1987-07-01  |  13KB  |  286 lines

  1. O=====|====|====|====|====|====|====|====|====|====|====|====|====|====|====|===
  2. Page      
  3. PROGRAMMING IN APPLESOFT BASIC
  4. David C. Hunt
  5. I.  Introduction
  6. @BASIC is a language for computers.  Compared with natural human H
  7. Flanguages like English or French, it has a very small number of words E
  8. Cand grammatical rules.  BASIC stands for 
  9. eginners 
  10. ll-purpose G
  11. ymbolic 
  12. nstruction 
  13. ode.  It is not especially strong in any J
  14. Hparticular area, but it does a good many things well.  It is also quite J
  15. Heasy to learn and uses words similar to English.  BASIC is the language I
  16. Gwhich comes included with nearly all micro-computers, however, not all I
  17. Gversions are compatible.  A program written in Applesoft BASIC may not 5
  18. run in Commodore BASIC for example, and vice versa.
  19. ----------------------------------------------------
  20. II.  Starting BASIC
  21. GApplesoft BASIC is built into the Apple IIe computer, however we would J
  22. Hnot be able to save our programs without first loading a Disk Operating H
  23. FSystem (DOS).  To start working in BASIC with the computer off, first J
  24. Hinsert the Apple System Master program disk (DOS 3.3), then turn on the E
  25. CApple power switch.  The disk drive light will go on.  After a few J
  26. Hmoments the light will go off, the drive will stop and your screen will 
  27. display the message:
  28. BE SURE CAPS LOCK IS DOWN
  29. CThe square bracket "]" is a 
  30. prompt
  31. , meaning that the computer is I
  32. Gwaiting for your instructions.  The flashing rectangle is the 
  33. cursor
  34. and indicates where the next character you type will appear.
  35. ----------------------------------------------------
  36. III.  Printing with BASIC
  37. Now type:
  38. PRINT "HELLO THERE"
  39. HBe sure to include the quotation marks and press <Return>!  If you make F
  40. Da mistake you may use the left and right arrow keys to correct your K
  41. Ityping BEFORE pressing <Return>.  The <Return> key tells the computer to E
  42. Caccept the line you just typed and act on it.  The computer should J
  43. Hrespond by printing your message.  Anything included in quotation marks K
  44. Ifollowing the command PRINT will be printed literally on the screen.  If K
  45. Iyou mispel PRINT you will get the error message "?SYNTAX ERROR".  If you K
  46. Iomit the quotation marks the computer will think you are refering to the ?
  47. name of a variable (more on that later) and PRINT a zero (0).
  48. Try some arithmetic:
  49. PRINT 5 + 5
  50. PRINT 321-123
  51. PRINT 4/2
  52. PRINT 4*2
  53. IThe computer uses the * to indicate multiplication and the / to indicate 
  54. division.
  55. Let's get rid of all this garbage.  Now type:
  56. We can imbed punctuation marks to control the format of printing.  Try:
  57. PRINT "HELLO";"THERE"
  58. then,
  59. PRINT "HELLO","THERE"
  60. AThe semicolon (;) tells the computer to hold the cursor position K
  61. Iimmediately after printing.  That is why there is no space between words J
  62. Hin 'HELLOTHERE'.  The comma (,) tells the computer to print at the next L
  63. J"tab zone".  It will space over to a preset tab distance and then print.  J
  64. HThat is why there are so many spaces between "Hello" and "There" in the J
  65. Hsecond example.  Note that if these characters were included within the J
  66. quotation marks they would be printed "as is" and have no format effect.
  67. ----------------------------------------------------
  68. IV.  Programs
  69. Type the command:   NEW.
  70. Type the command:   RUN.
  71. HThe computer responds by doing nothing.  It has no program in memory to 3
  72. RUN.  We can store a set of commands in programs.
  73. 10 PRINT "HELLO THERE"
  74. 20 PRINT "BYE NOW"
  75. 30 END
  76. DIf you make a mistake, retype the entire line.  Some computers will H
  77. Fallow you to move anywhere on the screen with the arrow keys and make 5
  78. corrections.  The Apple does not have this feature.
  79. HBe sure to use line numbers.  This is how the computer knows that these H
  80. Fcommands will be executed later and not immediately after being typed J
  81. Hin.  We have numbered by tens so that we will have room in case we need F
  82. Dto make changes and add other lines in between later.  Now run this !
  83. little program by typing:   RUN
  84. This time BASIC responds:
  85. HELLO THERE    
  86. BYE NOW
  87. We can have this program run itself several times successively.  Try:
  88. 5 REM   MY FIRST PROGRAM
  89. 30 GOTO 10
  90. 40 END
  91. Now type the command:   LIST
  92. IThe computer will respond by listing your program on the screen.  Notice G
  93. Ethat the new line #30 has replaced the old and lines #10 and 40 have J
  94. Hbeen added.  The lines are also listed in correct numerical order.  The J
  95. HREM statement in line 10 simply means REMark.  It is a place for you to K
  96. Imake comments for yourself.  The computer will ignore any line beginning 
  97. with REM.
  98. Now type:   RUN
  99. HThe program will print the statements in lines 10 and 20.  Then line 30 H
  100. Ftells it to go back to line 10 and it becomes an "infinite loop".  Do I
  101. Gyou see why the program will never reach line 40?  If you wish to stop J
  102. Hthis procedure press CTRL-C.  You do this by holding down the <CONTROL> (
  103. key and pressing <C> at the same time.
  104. ----------------------------------------------------
  105. V.  Saving and Loading
  106. If your own disk is in the drive, you can save your programs.
  107. Type:  SAVE PROGRAM1
  108. >The most recent program in memory is now saved under the name 
  109. "PROGRAM1".
  110. To see what is on your disk type:   CATALOG
  111. GIf you see that your program has been saved, then clear the computer's 
  112. memory by typing:  NEW
  113. Now LIST.  There should be nothing to LIST in memory.
  114. FAt some later time you can then retrieve these programs from disk and "
  115. use them again without retyping.
  116. Type:  LOAD PROGRAM1
  117. GWhen the disk drive light goes out type LIST.  Your program is back in 
  118. memory again!
  119. VI.  Variables
  120. HVariables are like little mailboxes in the computer's memory.  Each box E
  121. Ccan hold a piece of information you put in it.  The mailboxes have J
  122. Hrather strange names rather than numbers.  The name indicates what kind !
  123. of information is stored there.
  124. IFor example, we might have a value stored in a mailbox, or a "string" of A
  125. letters, numbers and other characters.  We can do this like so:
  126. A = 1
  127. A$ = "HELLO THERE!"
  128. To prove that it worked type:  PRINT A
  129. Then try:   PRINT A$
  130. FThe contents of those variables is printed.  You do NOT use quotation &
  131. marks around the name of a variable!
  132. Try:   PRINT B
  133. There is nothing stored in mailbox "B" now so the result printed is "0".
  134. CA$ is pronounced "A-string".  The dollar sign ($) indicates that a <
  135. "string of characters" rather than a value is stored here.
  136. Variables can also be assigned within a program.
  137. Be sure to type NEW to clear any old programs from memory!  Then:
  138. 10 A$="ONE"
  139. 20 B$="TWO"
  140. 30 C$="THREE"
  141. 40 A=1
  142. 50 B=2
  143. 60 C=3
  144. 70 PRINT A$,A
  145. 80 PRINT B$,B
  146. 90 PRINT C$,C    
  147. 100 END
  148. HTry to figure out what this program will print on the screen.  Then RUN 
  149. ----------------------------------------------------
  150. VII.  READ-DATA
  151. HThere is another way to put information into variables.  We can use the #
  152. pair of statements READ and DATA.
  153. HType NEW to clear the previous program from memory.  Type HOME to get a 
  154. clean screen to work with.
  155. 10 READ A$,B$,C$,A,B,C
  156. 20 DATA ONE,TWO,THREE,1,2,3)
  157. 30 PRINT A$,A : PRINT B$,B : PRINT C$,C
  158. 40 END
  159. JThis program will have the same effect as the last, but it much shorter.  I
  160. GIt is a better use of memory.  Notice how the colon (:) can be used to J
  161. Hseparate statements and allow them to be combined on one line.  Not all 
  162. versions of BASIC allow this.
  163. ----------------------------------------------------
  164. VIII.  FOR-NEXT
  165. DWe can create a progaram that will execute a given range of lines a G
  166. Especific number of times.  We do this with the pair FOR and NEXT.  A I
  167. Gvariable is used as a "counter" in the FOR line to keep track of where     
  168. we are.
  169. D10 PRINT "MY NAME IS .................":REM--INCLUDE YOUR NAME HERE 
  170. INSTEAD OF THE DOTS!
  171. 20 END
  172. RUN the program.  Then add the following lines.
  173. 5 FOR X = 1 TO 10 STEP 1
  174. 15 NEXT X
  175. LIST the program to be sure it is all there.
  176. GNow the program will assign the variable "X" the value "1" and execute I
  177. Gline 10.  At line 15 the program will branch back to line 5.  Variable G
  178. E"X" will have 1 added to it (the STEP) and have the value "2".  This G
  179. Econtinues until the value of the variable is 11.  When that point is D
  180. Breached in line 5 it no longer is in the range 1-10 so control is H
  181. transferred past the NEXT statement to line 20 where the program ENDs.
  182. Try retyping line 5 and increasing the range, say X=1 TO 100.
  183. ----------------------------------------------------
  184. IX.  INPUT
  185. FMicro-computers often are used in an "interactive" environment.  That F
  186. Dis, the user will enter information directly when asked.  The INPUT C
  187. statement tells the computer to accept information from the user.
  188. Clear memory with the NEW command.
  189. 10 INPUT NA$
  190. 20 PRINT NA$
  191. 30 END
  192. @When this is RUN, a question mark "?" prompt will appear.  This I
  193. Gindicates that the computer is waiting for some information from you.  J
  194. HIt will wait until you press <Return>.  That information will be stored G
  195. Ein the variable NA$.  INPUT will not allow the use of commas (,).  A ?
  196. comma will give you the unfriendly response "?EXTRA IGNORED".
  197. Add the following for greater clarity:
  198. 5 PRINT "PLEASE ENTER YOUR NAME";
  199. FA prompt line may be included in the request for input.  Clear memory  
  200. before enterring this program:
  201. 10 HOME#
  202. 20 INPUT "WHAT IS YOUR NAME?";NA$
  203. 30 PRINT
  204. 40 PRINT "HELLO, ";NA$
  205. 50 END
  206. INote that line 30 does nothing more than print a blank space to increase ?
  207. readability.  Line 10 clears the screen for the same purpose.
  208. ----------------------------------------------------
  209. X.  IF-THEN
  210. HA computer can appear to make decisions or posses intelligence by using I
  211. Gthe IF and THEN commands.  Insert YOUR NAME in place of the dots (...) 
  212. in this example.
  213. 10 HOME#
  214. 20 PRINT "HALT!  WHO GOES THERE?"
  215. 30 INPUT NA$
  216. 40 PRINT=
  217. 50 IF NA$ = ".........." THEN PRINT "WELCOME, ",NA$:GOTO 70H
  218. 60 IF NA$ <> ".........." THEN PRINT "I DON'T KNOW YOU.  GOODBYE.":END*
  219. 70 PRINT: PRINT "LET'S PLAY SOME GAMES!"/
  220. 80 REM  THE REST OF THE PROGRAM WOULD GO HERE
  221. 90 END
  222. INow when the program gets to line 50-60 it will check to see if the name D
  223. Byou gave at the INPUT prompt matches the name it is programmed to I
  224. Gaccept.  It so (line 50), then it will greet you and skip ahead to the I
  225. Grest of the program.  If the names differ it drops through to line 60, '
  226. prints the Goodbye message and quits.
  227. HLet's add a few more lines to this same program, again using INPUT.  Do 
  228. NOT type NEW.
  229. 80 PRINT "ENTER 'C' TO CONTINUE"
  230. 90 INPUT C$
  231. 100 IF C$ = "C" THEN GOTO 10
  232. 110 IF C$ <> "C" THEN END
  233. DIn this case by entering the letter "C" (followed by <Return>) when J
  234. Hasked the program will start over from the beginning.  Any other letter H
  235. Fwill end the program.  Try this program several times.  Use your name "
  236. first, then try some other name.
  237. ----------------------------------------------------
  238. On Your Own  
  239. 10 REM THIS PROGRAM WILL PRINT A UNIQUE VERSION OF HUMPTY DUMPTY
  240. 20 PRINT:PRINT
  241. 30 PRINT "ENTER A VERB:"
  242. 40 INPUT A$
  243. 50 PRINT "ENTER A NOUN"
  244. 60 INPUT B$ 
  245. 70 PRINT "ENTER AN ADJECTIVE:"
  246. 80 INPUT C$
  247. 90 PRINT "ENTER A NOUN"
  248. 1O0 INPUT D$"
  249. 110 PRINT "ENTER A PLURAL NOUN:"
  250. 120 INPUT E$"
  251. 130 PRINT "ENTER A PLURAL NOUN:"
  252. 140 INPUT F$
  253. 150 PRINT "ENTER A VERB:"
  254. 160 INPUT G$
  255. 170 PRINT:PRINT:PRINT:PRINT,
  256. 180 PRINT "A NEW VERSION OF HUMPTY DUMPTY"
  257. 190 PRINT:PRINT+
  258. 200 PRINT "HUMPTY DUMPTY ";A$;" ON A ";B$,
  259. 210 PRINT "HUMPTY DUMPTY HAD A ";C$;" ";D$:
  260. 220 PRINT "ALL THE KING'S ";E$;" AND ALL THE KING'S ";F$4
  261. 230 PRINT "COULDN'T ";G$;" HUMPTY TOGETHER AGAIN."    
  262. 240 END
  263. 10 PRINT "THIS SAMPLE PROGRAM WILL ILLUSTRATE HOW A COMPUTER CAN<
  264. 15 PRINT "BE USED TO ASK QUESTIONS AND RESPOND TO ANSWERS"
  265. 20 PRINT:PRINT
  266. 30 PRINT "WHAT IS YOUR NAME";
  267. 40 INPUT N$
  268. 50 PRINT:PRINTE
  269. 60 PRINT "WHO WAS THE FIRST PRESIDENT OF THE U.S. (LAST NAME ONLY)"
  270. 70 INPUT B$
  271. 80 PRINT:PRINTD
  272. 90 IF B$="WASHINGTON" THEN PRINT "GOOD SHOW ";N$;"-THAT'S CORRECT"B
  273. 100 IF B$<>"WASHINGTON" THEN PRINT "TOO BAD ";N$;"-THAT'S WRONG"    
  274. 110 END
  275. 10 PRINT "THIS PROGRAM WILL FIND THE AREA AND PERIMETER OF A SQUARE"
  276. 20 PRINT:PRINT5
  277. 30 PRINT "ENTER THE LENGTH OF THE SIDE OF A SQUARE"
  278. 35 INPUT S
  279. 40 LET A=S^2
  280. 50 LET P=4*S
  281. 60 PRINT:PRINT:PRINT:PRINT)
  282. 70 PRINT "THE AREA OF THE SQUARE IS ";A
  283. 80 PRINT.
  284. 90 PRINT "THE PERIMETER OF THE SQUARE IS ";P    
  285. 100 END
  286.